import orderService from "@/apis/order";
import { formattedGMTDate } from "@/utils/util";

// pages/order/list/list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 静态数据绑定
    activeTab: 0,

    // 动态数据绑定
    tabs: [
      { title: "全部(0)", id: 0 },
      { title: "待报价(0)", id: 1 },
      { title: "待付款(0)", id: 2 },
      { title: "已付款(0)", id: 3 },
      { title: "战败(0)", id: 4 }
    ],
    list: [],
    orders: [],
    unpays: [],
    pays: [],
    fails: [],
    unprices:[],
    cname:'',
    showClear: false,
  },
  // 切换标签事件
  onTabChange(event) {
    const index = event.detail.index;
    this.setData({
      activeTab: index
    });
  },
  // 修改输入事件处理
  onSearchInput(e) {
    const value = e.detail.value;
    this.setData({
        cname: value,
        showClear: value.trim().length > 0
    });
    this.initOrders(this.data.list);
  },
  async loadOrders(){
    const res=await orderService.list_user_id()
    if(res.data.code==404){
      wx.showToast({
        title: '暂无订单',
        icon: 'none',
        duration: 2000
      })
      return 
    }
    this.initOrders(res.data.data);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const app=getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
    // 模拟服务端数据获取
    this.loadOrders()
  },
  initOrders(list) {
    list = list.map(o => { o.created_at = formattedGMTDate(o.created_at) ; return o})
    const orders=this.data.cname==''?list:list.filter(order => order.cname.indexOf(this.data.cname)!=-1)
    const unpays = orders.filter(order => order.order_status == 0);
    const pays = orders.filter(order => order.order_status == 1);
    const fails = orders.filter(order => order.order_status == 2);
    const unprices = orders.filter(order => order.order_status == -1);
    this.setData({
      list,
      orders,
      unpays,
      pays,
      fails,
      unprices,
      tabs:[
        { title: "全部(" + orders.length + ")", id: 0 },
        { title: "待报价("+unprices.length+")", id: 1},
        { title: "待付款(" + unpays.length + ")", id: 2 },
        { title: "已付款(" + pays.length + ")", id: 3 },
        { title: "战败(" + fails.length + ")", id: 4 }
      ]
    }); 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const app = getApp();
    app.checkToken().then(valid => {
      console.log("order/list.js onShow() token验证结果: " + valid);
    });
    this.loadOrders()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    const app=getApp()
    wx.setKeepScreenOn({
      keepScreenOn: app.globalData.isRecording,
      success: () => console.log('常亮已启用')
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // 显示加载动画
    wx.showLoading({
      title: '加载中...',
    });
    // 重新加载数据
    orderService.list_user_id().then(res => {
      this.initOrders(res.data.data);
    }).catch(err => {
      console.log(err)
    }).finally(() => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    wx.showToast({
      title: '没有更多数据了',
      icon: 'loading',
      duration: 2000
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})